﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;

namespace BT_DoHoa_Nhom13.GraphicLib
{
    public class PNGGDIPlusGraphic : MyGraphic
    {
        Graphics graphics;
        String filename;
        int imgWidth, imgHeight;
        Bitmap bm;
        static MyGraphic instance;
        public static MyGraphic GetInstance(String path, int panelWidth, int panelHeight)
        {
            if (instance == null)
            {
                instance = new  PNGGDIPlusGraphic(path, panelWidth,panelHeight);
            }

            return instance;
        }

        private PNGGDIPlusGraphic(String path, int panelWidth, int panelHeight)
        {
            name = ".PNG GDI+ Graphic";
            filename = path;
            imgWidth = panelWidth;
            imgHeight = panelHeight;
            bm = new Bitmap(panelWidth, panelHeight);
            graphics = Graphics.FromImage(bm);
        }

        public override void SetGraphicInfo(Graphics graphic, string path)
        {
            filename = path;
        }

        public override void DrawLine(int x1, int y1, int x2, int y2)
        {
            graphics.DrawLine(Pens.Black, x1, y1, x2, y2);
            bm.Save(filename, ImageFormat.Png);
        }

        public override void DrawEllipse(int x1, int y1, int width, int height, Boolean isFill)
        {
            if (isFill)
            {
                graphics.FillEllipse(Brushes.Black, x1, y1, width, height);
            }
            else
            {
                graphics.FillEllipse(Brushes.White, x1, y1, width, height);
                graphics.DrawEllipse(Pens.Black, x1, y1, width, height);
            }

            bm.Save(filename, ImageFormat.Png);
        }

        public override void DrawRectangle(int x1, int y1, int width, int height, bool isFill)
        {
            if (isFill)
            {
                graphics.FillRectangle(Brushes.Black, x1, y1, width, height);
            }
            else
            {
                graphics.FillRectangle(Brushes.White, x1, y1, width, height);
                graphics.DrawRectangle(Pens.Black, x1, y1, width, height);
            }

            bm.Save(filename, ImageFormat.Png);
        }

        public override void DrawString(int x1, int y1, int width, int height, string content)
        {
            StringFormat stringFormat = new StringFormat();
            stringFormat.Alignment = StringAlignment.Center;
            stringFormat.LineAlignment = StringAlignment.Center;
            graphics.DrawString(content,
                               new Font(FontFamily.GenericSansSerif, 10, FontStyle.Regular),
                               Brushes.Black,
                               new Rectangle(x1, y1, width, height), stringFormat);

            bm.Save(filename, ImageFormat.Png);
        }

        public override void DrawArc(int x, int y, int width, int height, float angleStart, float angleSweep)
        {
            if (width > 10 && height > 10)
            {
                graphics.DrawArc(Pens.Black, new Rectangle(x, y, width, height), angleStart, angleSweep);
                bm.Save(filename, ImageFormat.Png);
            }
        }


        public override void DrawRoundRectangle(int x, int y, int width, int height, int radius, Boolean isFill)
        {
            Rectangle roundedRectangle = Rectangle.Round(new Rectangle(x, y, width, height));
            graphics.DrawLine(Pens.Black, x + radius, y, x + width - (radius), y);
            graphics.DrawArc(Pens.Black, x + width - (radius * 2), y, radius * 2, radius * 2, 270, 90);
            graphics.DrawLine(Pens.Black, x + width, y + radius, x + width, y + height - (radius));
            graphics.DrawArc(Pens.Black, x + width - (radius * 2), y + height - (radius * 2), radius * 2, radius * 2, 0, 90);

            graphics.DrawLine(Pens.Black, x + width - (radius), y + height, x + radius, y + height); // Line
            graphics.DrawArc(Pens.Black, x, y + height - (radius * 2), radius * 2, radius * 2, 90, 90); // Corner
            graphics.DrawLine(Pens.Black, x, y + height - (radius), x, y + radius); // Line
            graphics.DrawArc(Pens.Black, x, y, radius * 2, radius * 2, 180, 90); // Corner

            bm.Save(filename, ImageFormat.Png);
        }
    }
}
